﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Phoenix.Backend
{
    public partial class Contact : System.Web.UI.Page
    {
        //protected DataTable dt;
        Phoenix.AppCode.Entities.Contact contact = new Phoenix.AppCode.Entities.Contact();
        string success = "<div class='alert_success'><p><img src='../Content/Backend/images/icon_accept.png' alt='success' class='mid_align'>" + "Danh mục được thêm mới thành công." + "</p></div>";
        string error = "<div class='alert_error'><p><img src='../Content/Backend/images/icon_error.png' alt='error' class='mid_align'>" + "Máy chủ đang bận. Vui lòng thử lại." + "</p></div><br class='clear'/>";
        //protected void Page_PreInit(object sender, EventArgs e)
        //{
        //    if (Session["Theme"].ToString() != "")
        //    {
        //        Page.Theme = Session["Theme"].ToString();
        //    }
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    //dt = Phoenix.AppCode.Bussiness.ContactService.ContactList();

                    if (Request.QueryString["Action"] == "Edit" || Request.QueryString["Action"] == "Delete")
                    {
                        edit.Visible = false;
                        Load_ActionContact(Request.QueryString["Action"]);
                    }
                }
                catch (FormatException)
                {
                    Response.Redirect("Error.aspx");
                }
            }
        }

        #region  Load action Contact
        private void Load_ActionContact(string action)
        {
            try
            {
                //DirectoryInfo dirLanguage = new DirectoryInfo(Server.MapPath(@"~\" + @"Languages"));
                if (action == "Edit")
                {
                    Phoenix.AppCode.Bussiness.ContactService.Edit(Phoenix.AppCode.Bussiness.ContactService.GetContactByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim())));
                    
                    edit.Visible = true;
                    list.Visible = true;

                    DataTable dt_Edit = Phoenix.AppCode.Bussiness.ContactService.ContactList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                    lblFullName.Text = dt_Edit.Rows[0]["FullName"].ToString();
                    lblEmail.Text = dt_Edit.Rows[0]["Email"].ToString();
                    lblSubject.Text = dt_Edit.Rows[0]["Subject"].ToString();
                    lblContent.Text = dt_Edit.Rows[0]["Content"].ToString();
                    lblPostDate.Text = DateTime.Parse(dt_Edit.Rows[0]["PostDate"].ToString()).ToString("hh:mm tt - dd/MM/yyyy");
                    lblReplyMail.Text = "<a class='button' href='mailto:" + dt_Edit.Rows[0]["Email"].ToString() + "'>Trả lời mail</a>";
                    
                }
                else if (action == "Delete")
                {
                    try
                    {
                        contact = Phoenix.AppCode.Bussiness.ContactService.GetContactByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.ContactService.Delete(contact);
                        lblWarning.Text = success;
                        //Response.Redirect("Contact.aspx");
                    }
                    catch
                    {
                        lblWarning.Text = error;
                        //Response.Redirect("Error.aspx");
                    }
                }
                else
                {
                    edit.Visible = false;
                    list.Visible = true;
                }
            }
            catch
            {
            }
        }
        #endregion
    }
}